<?php

namespace app\store\controller\order;

use app\store\controller\BaseController;
use app\store\service\OrderService;
use app\store\service\PaymentService;
use think\facade\Db;
use think\facade\View;

class OrderController extends BaseController
{
    public function index()
    {
        $param = $this->request->param();
        $list = OrderService::getOrderList($param);

        return View::fetch('',['list' => $list]);
    }

    /**
     *  待发货
     * */
    public function delivery()
    {
        $param = $this->request->param();
        $list = OrderService::getDeliveryList($param);

        return View::fetch('',['list' => $list]);
    }

    /**
     *  待收货
     * */
    public function receipt()
    {
        $param = $this->request->param();
        $list = OrderService::getReceiptList($param);


        return View::fetch('',['list' => $list]);
    }

    /**
     *  待付款
     * */
    public function pay()
    {
        $param = $this->request->param();
        $list = OrderService::getPayList($param);

        return View::fetch('',['list' => $list]);
    }


    /**
     *  待付款
     * */
    public function finish()
    {
        $param = $this->request->param();
        $list = OrderService::getFinishList($param);


        return View::fetch('',['list' => $list]);
    }


    /**
     *  已取消
     * */
    public function cancel()
    {
        $param = $this->request->param();
        $list = OrderService::getCancelList($param);


        return View::fetch('',['list' => $list]);
    }


    /**
     *  待付款
     * */
    public function after()
    {
        $param = $this->request->param();
        $list = OrderService::getAfterSaleList($param);


        return View::fetch('',['list' => $list]);
    }

    public function read()
    {
        $order_id = $this->request->param('order_id/d');
        $row = OrderService::findOrder($order_id);
        //  print_r($row);exit;
        return View::fetch('',['row' => $row]);
    }

    public function sendDelivery()
    {
        
        $store_id = session('store_id');
        $order_id = $this->request->param('order_id/d');
        $courier_number = $this->request->param('courier_number');
        $row = Db::name('order')->where('id', $order_id)->where('id', $order_id)->where('store_id', $store_id)->find();
        if (empty($row)) {
            return json(['code' => 0, 'msg' => '订单不存在!']);
        }
        if ($row['status'] !=2 ) {
            return json(['code' => 0, 'msg' => '订单不能发货!']);
        }
        $rst = Db::name('order')->where('id', $order_id)->update(['status'=>3, 'courier_number'=>$courier_number]);
        if ($rst) {
            return json(['code' => 1, 'msg' => '发货成功!']);
        } else {
            return json(['code' => 0, 'msg' => '发货失败!']);
        }
       
    }

  
    public function refund()
    {
        $order_id = $this->request->param('order_id/d');

        $order = OrderService::findRefundOrder($order_id);

        $config = PaymentService::findPaymentConfig($order['store_id'],$order['payment_type']);



        $result = PaymentService::paymentQuery($order_id);

        return $result;
    }

    public function query()
    {

    }
}